package ca.ericslandry.client;

import net.customware.gwt.presenter.client.place.PlaceManager;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;
import ca.ericslandry.client.gin.Injector;
import ca.ericslandry.client.mvp.presenter.IssueListPresenter;
import ca.ericslandry.client.mvp.presenter.MainPresenter;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * GWT entry point
 */
public class GwtApp implements EntryPoint {
	public void onModuleLoad() {
		Log.debug("onModuleLoad starts here.");
		GwtUtils.removeElementFromDOM("loading");
		Window.setMargin("0px");
		Injector injector = GWT.create(Injector.class);
		Log.debug("onModuleLoad Injector created.");
		MainPresenter mainPresenter = injector.getMainPresenter();
		mainPresenter.bind();
		RootLayoutPanel.get().add(mainPresenter.getDisplay().asWidget());

		// Load PlaceManager so it can start listening
		PlaceManager placeManager = injector.getPlaceManager();
		String currentPlace = History.getToken();
		if ("".equals(currentPlace)) {
			// Nothing in URL, load default page
			injector.getEventBus().fireEvent(new PlaceRequestEvent(new PlaceRequest(IssueListPresenter.PLACE)));
		} else {
			// Load requested page
			placeManager.fireCurrentPlace();
		}
	}
}